#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "rawimage.h"
#define MATRIX(matrix,r,c,width) matrix[r*width+c]
int K;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    K = 0;
    ui->setupUi(this);
    QObject::connect(ui->raw_image_label,SIGNAL(wheelSignals(QWheelEvent*)),this,
                     SLOT(on_raw_image_label_wheelSignal(QWheelEvent*)));
    QObject::connect(ui->raw_image_label,SIGNAL(mouseMoveSignal(QMouseEvent*)),this,
                     SLOT(label_mouse_move(QMouseEvent*)));
    QObject::connect(ui->raw_image_label,SIGNAL(leaveSignal()),this,
                     SLOT(label_leave()));
    bin_operation = AddOperation;
    zoom_mode = 0;
    this->statusBar()->showMessage(QString("").sprintf("Color:R[     ] G[     ] B[     ]"));

    for(int i = 0;i<9;i++)
    {
        matrix[i] = 1;
    }
    matrix[MATRIX_HEIGHT] = 3;
    matrix[MATRIX_WIDTH] = 3;
    isBin = false;
    //ui->tableWidget->setRowCount(matrix[MATRIX_HEIGHT]);
    //ui->tableWidget->setColumnCount(matrix[MATRIX_WIDTH]);
    for(int i = 0;i<matrix[MATRIX_HEIGHT];i++)
        for(int j = 0;j<matrix[MATRIX_WIDTH];j++)
        {
            QString str = QString::number(matrix[i*(int)matrix[MATRIX_WIDTH]+j]);
    }
}

MainWindow::~MainWindow()
{
    delete ui;

}

void MainWindow::label_mouse_move(QMouseEvent *ev)
{

    QRgb color = QPixmap::grabWidget(ui->raw_image_label).toImage().pixel(ev->pos());
    this->statusBar()->showMessage(QString("").sprintf("Color:R[  %d\t  ] G[  %d\t  ] B[  %d\t  ]",
                                                       qRed(color),
                                                       qGreen(color),
                                                       qBlue(color)));
}

void MainWindow::SetImage(QImage image)
{
    raw_image = image;
    //qDebug("%d %d\n",image.width(),image.height());
    GetGrayHist(hist,&raw_image);
    this->ui->widget->SetHistogram(hist);
    this->ui->raw_image_label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::SetSecondImage(QImage image)
{
    second_image = image;
    this->ui->preview_label->setPixmap(QPixmap::fromImage(second_image.scaled(115,78,Qt::KeepAspectRatio)));
}

void MainWindow::on_actionOpen_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                     tr("Open Image File"),".",
                                                     tr("All Files(*)(*.*)\n"
                                                        "BMP Images(*.bmp)\n"
                                                        "JPG Images(*.jpg *.jpeg)\n"
                                                        "GIF Images(*.gif)\n"
                                                        "PNG Images(*.png)\n")
                                                     );

     if(!fileName.isEmpty()){
         if(!raw_image.load(fileName))
         {
             QMessageBox::information(this,tr("Image Processor"),tr("Cannot load %1.").arg(fileName));
             return;

         }
     }
     this->ui->tabWidget->setEnabled(true);
     SetImage(raw_image.convertToFormat(QImage::Format_RGB888));
     raw_size = raw_image.size();

}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{

}





void MainWindow::on_actionShow_Option_View_toggled(bool arg1)
{
    this->ui->option_dock->setShown(arg1);
}

void MainWindow::on_actionGray_Hist_View_toggled(bool arg1)
{
    this->ui->dockWidget->setShown(arg1);
}





void MainWindow::on_pushButton_7_clicked()
{
   // GreyImage(&this->raw_image);
    SetImage(PointImageOperation(&raw_image,
                                 GrayOperation));
}

void MainWindow::on_pushButton_clicked(bool checked)
{
    float matrix[9] = {0,0,0,
                       0,-1,0,
                       0,0,1};
    SetImage(ConvImage(&raw_image,matrix,3,3));
}

void MainWindow::on_pushButton_3_clicked()
{
    float matrix[25] = {1,4,7,4,1,
                       4,16,26,16,4,
                       7,26,41,26,7,
                       4,16,26,16,4,
                       1,4,7,4,1};
    SetImage(ConvImage(&raw_image,matrix,5,5,273));
}

void MainWindow::on_pushButton_4_clicked()
{
    float matrix[9] = {1,1,1,
                       1,1,1,
                       1,1,1};
    SetImage(ConvImage(&raw_image,matrix,3,3,9));
}

void MainWindow::on_pushButton_2_clicked()
{
    int factor = 1;
    float matrix1[9] = {1,1,1,
                        0,0,0,
                        -1,-1,-1};
    float matrix2[9] = {1,0,-1,
                        1,0,-1,
                        1,0,-1};
    SetImage(BinaryImageOperation(
                 &ConvImage(&raw_image,matrix1,3,3),
                 &ConvImage(&raw_image,matrix2,3,3),
                 MultiOperation,&factor));
}

void MainWindow::on_pushButton_5_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                     tr("Open Image File"),".",
                                                     tr("All Files(*)(*.*)\n"
                                                        "BMP Images(*.bmp)\n"
                                                        "JPG Images(*.jpg *.jpeg)\n"
                                                        "GIF Images(*.gif)\n"
                                                        "PNG Images(*.png)\n")
                                                     );

     if(!fileName.isEmpty()){
         if(!second_image.load(fileName))
         {
             QMessageBox::information(this,tr("Image Processor"),tr("Cannot load %1.").arg(fileName));
             return;

         }
     }
     this->ui->pushButton_6->setEnabled(true);
     SetSecondImage(second_image.convertToFormat(QImage::Format_RGB888));
}

void MainWindow::on_comboBox_currentIndexChanged(int index)
{
    switch(index)
    {
    case 0:
        bin_operation = AddOperation;
        break;
    case 1:
        bin_operation = MultiOperation;
        break;
    case 2:
        bin_operation = SubOperation;
        break;
    case 3:
        break;

    }
}

void MainWindow::on_pushButton_6_clicked()
{
    int factor = 1;
    SetImage(BinaryImageOperation(&raw_image,&second_image,bin_operation,&factor));
}

void MainWindow::on_raw_image_label_wheelSignal(QWheelEvent *arg)
{
    if(ui->zoom_silder->isEnabled())
    {
        if(arg->delta()>0)
            ui->zoom_silder->setValue(ui->zoom_silder->value()+ui->zoom_silder->pageStep());
        else
            ui->zoom_silder->setValue(ui->zoom_silder->value()-ui->zoom_silder->pageStep());
    }
}

void MainWindow::on_pushButton_10_clicked()
{
    int factor = 1;
    float matrix1[9] = {1,2,1,
                        0,0,0,
                        -1,-2,-1};
    float matrix2[9] = {1,0,-1,
                        2,0,-2,
                        1,0,-1};
    SetImage(BinaryImageOperation(
                 &ConvImage(&raw_image,matrix1,3,3),
                 &ConvImage(&raw_image,matrix2,3,3),
                 MultiOperation,&factor));
}

void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
    this->zoom_mode = index;
}

void MainWindow::on_zoom_silder_valueChanged(int value)
{
    SetImage(ZoomImage(&raw_image,raw_size.height()*value/100,raw_size.width()*value/100,zoom_mode));
}

void MainWindow::on_pushButton_8_clicked()
{
    GetGrayHist(this->hist,&raw_image);
    RawImage BinDialog(&raw_image,hist,this);
    BinDialog.show();
    if(BinDialog.exec())
    {
        int args = BinDialog.getBin();
        if(BinDialog.isEq)
            raw_image=PointImageOperation(&raw_image,
                                RemapOperation,BinDialog.map);
        if(args>=0)
            raw_image = PointImageOperation(&raw_image,BinOperation,&args);
        SetImage(raw_image);
    }
}

void MainWindow::on_pushButton_3_clicked(bool checked)
{

}

void MainWindow::on_pushButton_9_clicked()
{
    SetImage(MedianFilter(&raw_image));
}

void MainWindow::on_pushButton_11_clicked()
{
    int factor = 10;
    SetImage(PointImageOperation(&raw_image,ContrastOperation,&factor));
}

void MainWindow::on_pushButton_12_clicked()
{
    int factor = -10;
    SetImage(PointImageOperation(&raw_image,ContrastOperation,&factor));
}

void MainWindow::on_pushButton_13_clicked()
{
    int factor = 10;
    SetImage(PointImageOperation(&raw_image,LightOperation,&factor));
}

void MainWindow::on_pushButton_14_clicked()
{
    int factor = -10;
        SetImage(PointImageOperation(&raw_image,LightOperation,&factor));
}

void MainWindow::on_pushButton_15_clicked()
{
    float hist_second[256];
    GetGrayHist(hist_second,&this->second_image);
}

void MainWindow::on_pushButton_16_clicked()
{

    float matrix[4] = {1,1,1,
                       1};
    DilationErosion p = DilationErosion(true);
    SetImage(ConvImage(&raw_image,matrix,2,2,&p));
   // SetImage(MorReconstruction(&raw_image,matrix,3,3));


}

void MainWindow::on_pushButton_18_clicked()
{
    DilationErosion p = DilationErosion(false,isBin);
    SetImage(
                ConvImage(&raw_image,matrix,matrix[MATRIX_WIDTH],matrix[MATRIX_HEIGHT],&p)
                );


}

void MainWindow::on_pushButton_17_clicked()
{
    DilationErosion p = DilationErosion(true,isBin);
    SetImage(
                ConvImage(&raw_image,matrix,matrix[MATRIX_WIDTH],matrix[MATRIX_HEIGHT],&p)
                );


}


void MainWindow::on_pushButton_21_clicked()
{
    SetImage(Skeleton(&raw_image,&K,matrix,matrix[MATRIX_WIDTH],matrix[MATRIX_HEIGHT]));
}



void MainWindow::on_pushButton_22_clicked()
{
    SetImage(SkeletonRestruction(&raw_image,K,matrix,matrix[MATRIX_WIDTH],matrix[MATRIX_HEIGHT]));
}

void MainWindow::on_pushButton_19_clicked()
{
    SetImage(
                OpenImage(&raw_image,matrix,matrix[MATRIX_HEIGHT],matrix[MATRIX_HEIGHT],isBin)
                );
}

void MainWindow::on_pushButton_20_clicked()
{
    SetImage(
                CloseImage(&raw_image,matrix,matrix[MATRIX_HEIGHT],matrix[MATRIX_HEIGHT],isBin)
                );
}

void MainWindow::TableToMatrix()
{
    matrix[MATRIX_HEIGHT] = ui->tableWidget->rowCount();
    matrix[MATRIX_WIDTH] = ui->tableWidget->columnCount();
    for(int i =0;i<matrix[MATRIX_HEIGHT];i++)
    {
        for(int j = 0;j<matrix[MATRIX_WIDTH];j++)
        {

            if(ui->tableWidget->item(i,j)==0)
                matrix[i*(int)matrix[MATRIX_WIDTH]+j]  = 0;
            else
                matrix[i*(int)matrix[MATRIX_WIDTH]+j] =atoi( ui->tableWidget->item(i,j)->text().toStdString().c_str());
        }
    }
}

void MainWindow::on_pushButton_24_clicked()
{
    if(!this->ui->Kernel->isEnabled())
    {
        ui->Kernel->setEnabled(true);
        ui->pushButton_24->setText("Apply");
    }
    else
    {
        ui->Kernel->setEnabled(false);
        ui->pushButton_24->setText("Edit");
        TableToMatrix();
    }
}

void MainWindow::on_spinBox_valueChanged(int arg1)
{
    ui->tableWidget->setRowCount(arg1);
}

void MainWindow::on_spinBox_2_editingFinished()
{

}

void MainWindow::on_spinBox_2_valueChanged(int arg1)
{

    ui->tableWidget->setColumnCount(arg1);
}

void MainWindow::on_pushButton_23_clicked()
{
    SetImage(
                DistanceTrans(&raw_image)
            );
}

void MainWindow::on_comboBox_3_currentIndexChanged(int index)
{
    if(index == 0)
        isBin = false;
    else
        isBin = true;
}
